package com.lvshi0797.cms.web;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.lvshi0797.cms.entities.FManager;
import com.lvshi0797.cms.helper.BussinessException;

public class LogInterceptor extends HandlerInterceptorAdapter {
	@SuppressWarnings("unchecked")
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession();
		HandlerMethod hm = (HandlerMethod)handler;
		FManager user = (FManager)session.getAttribute("user");
		if(user==null) {
			response.sendRedirect(request.getContextPath()+"/login.jsp");
		} else {
			
			boolean isAdmin = (user.getIsAdmin()==1);
			String aname = hm.getBean().getClass().getName()+"."+hm.getMethod().getName();
			System.out.println("请求方法地址："+aname);
		/*	if(!isAdmin) {
				//不是超级管理人员，就需要判断是否有权限访问某些功能
				Set<String> actions = (Set<String>)session.getAttribute("allActions");
				String aname = hm.getBean().getClass().getName()+"."+hm.getMethod().getName();
				if(!actions.contains(aname)) throw new BussinessException("没有权限访问该功能");
			}*/
		}
		return super.preHandle(request, response, handler);
	}
}
